package com.wiseasy.smartpos.cashierdemo.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.wiseasy.smartpos.cashierdemo.DiagnosticInfoActivity;
import com.wiseasy.smartpos.cashierdemo.R;
import com.wiseasy.smartpos.cashierdemo.bean.LogBean;

import java.util.ArrayList;


public class LogListViewAdapter extends BaseAdapter {

    ArrayList<LogBean> logBeanArrayList = new ArrayList<LogBean>();
    Context context;

    public LogListViewAdapter(ArrayList<LogBean> logBeanArrayList,Context context){
        this.logBeanArrayList = logBeanArrayList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return logBeanArrayList.size();
    }

    @Override
    public LogBean getItem(int position) {
        return logBeanArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolderItem itemHolder;
        if (convertView == null) {
            itemHolder = new ViewHolderItem();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_simple, null);
            itemHolder.tv_title = (TextView) convertView.findViewById(R.id.content);
            convertView.setTag(itemHolder);
        } else {
            itemHolder = (ViewHolderItem) convertView.getTag();
        }

        String log = logBeanArrayList.get(position).log;
        itemHolder.tv_title.setText(log);
        itemHolder.tv_title.setTextColor(getColor(logBeanArrayList.get(position).colorIndex));
        if (!TextUtils.isEmpty(log) && log.contains("Click to get diagnostic information")){
            itemHolder.tv_title.setPaintFlags(itemHolder.tv_title.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
            itemHolder.tv_title.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, DiagnosticInfoActivity.class);
                    context.startActivity(intent);
                }
            });
        } else {
            itemHolder.tv_title.setPaintFlags(itemHolder.tv_title.getPaintFlags()& ~Paint.UNDERLINE_TEXT_FLAG);
            itemHolder.tv_title.setOnClickListener(null);
        }


        return convertView;
    }

    private static class ViewHolderItem {
        TextView tv_title;
    }

    public int getColor(int priority){
        int color = Color.rgb(0x00, 0x00, 0x00);
        switch (priority) {
            case 2:
                color = Color.rgb(0x00, 0x00, 0x00);//black
                break;
            case 3:
                color = Color.rgb(0x00, 0x70, 0xBB);//blue
                break;
            case 4:
                color = Color.rgb(0x48, 0xBB, 0x31);//green
                break;
            case 5:
                color = Color.rgb(0xBB, 0xBB, 0x23);//yellow
                break;
            case 7:
                color = Color.rgb(0xFF, 0x00, 0x06);//red
                break;
            default:
                color = Color.rgb(0x00, 0x00, 0x00);
                break;
        }
        return color;
    }
}
